package org.giovanolli.mumi.controller.business;

import java.util.ArrayList;

import org.giovanolli.mumi.controller.AbstractController;
import org.giovanolli.mumi.controller.AbstractModel;
import org.giovanolli.mumi.entities.Phone;
import org.giovanolli.mumi.entities.Skill;
import org.giovanolli.mumi.entities.Specialist;
import org.giovanolli.mumi.entities.SpecialistFee;
import org.giovanolli.mumi.entities.filter.AttentionTypeFilter;
import org.giovanolli.mumi.entities.filter.CityFilter;
import org.giovanolli.mumi.entities.filter.SkillFilter;
import org.giovanolli.mumi.entities.filter.SpecialistFilter;
import org.giovanolli.mumi.entities.filter.SpecialityFilter;
import org.giovanolli.mumi.service.AttentionTypeService;
import org.giovanolli.mumi.service.CityService;
import org.giovanolli.mumi.service.Service;
import org.giovanolli.mumi.service.SkillService;
import org.giovanolli.mumi.service.SpecialistService;
import org.giovanolli.mumi.service.SpecialityService;
import org.primefaces.event.FlowEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller("specialistController")
public class SpecialistController extends AbstractController<Specialist> {

	@Autowired
	private SpecialistService service;

	@Autowired
	private SkillService skillService;

	@Autowired
	private CityService cityService;

	@Autowired
	private SpecialityService specialityService;

	@Autowired
	private AttentionTypeService attentionTypeService;

	@Autowired
	private SpecialistModel model;

	public void init() {
		super.init();
		model.setFilter(new SpecialistFilter());
		model.setSelectedSkills(new ArrayList<Skill>());
		populateNonEntityPropertiesToModel();
	}

	@Override
	protected void bindModelToEntity() {
		Specialist editedSpecialist = model.getEditedEntity();
		editedSpecialist.setFirstName(model.getFirstName());
		editedSpecialist.setLastName(model.getLastName());
		editedSpecialist.setBirthDate(model.getBirthDate());
		editedSpecialist.setDocumentType(model.getDocumentType());
		editedSpecialist.setDocumentNumber(model.getDocumentNumber());
		editedSpecialist.setAddress(model.getAddress());
		editedSpecialist.setSpeciality(model.getSpeciality());
		editedSpecialist.getSkills().clear();
		editedSpecialist.getSkills().addAll(model.getSelectedSkills());
		editedSpecialist.setAttentionTimeDuration(model.getAttentionTimeDuration());
	}

	@Override
	protected void bindEntityToModel() {
		Specialist editedSpecialist = model.getEditedEntity();
		model.setFirstName(editedSpecialist.getFirstName());
		model.setLastName(editedSpecialist.getLastName());
		model.setBirthDate(editedSpecialist.getBirthDate());
		model.setDocumentType(editedSpecialist.getDocumentType());
		model.setDocumentNumber(editedSpecialist.getDocumentNumber());
		model.setAddress(editedSpecialist.getAddress());
		model.setSpeciality(editedSpecialist.getSpeciality());
		model.getSelectedSkills().clear();
		model.getSelectedSkills().addAll(editedSpecialist.getSkills());
		model.setAttentionTimeDuration(editedSpecialist.getAttentionTimeDuration());
	}

	@Override
	protected AbstractModel<Specialist> getModel() {
		return model;

	}

	protected Class<Specialist> getEntityClass() {
		return Specialist.class;
	}

	@Override
	public void populateNonEntityPropertiesToModel() {
		model.setSelectableSkills(skillService.getList(new SkillFilter()));
		model.setSelectableCities(cityService.getList(new CityFilter()));
		model.setSelectableSpecialities(specialityService.getList(new SpecialityFilter()));
		model.setSelectableAttentionTypes(attentionTypeService.getList(new AttentionTypeFilter()));
	}

	@Override
	protected Service<Specialist> getService() {
		return service;
	}

	// PHONE EVENTS

	public void addPhoneEvent() {
		model.setEditedPhone(new Phone());
	}

	public void confirmSavePhoneEvent() {
		Phone editedPhone = model.getEditedPhone();

		for (Phone phone : model.getEditedEntity().getPhones()) {
			if (phone.getUid().equals(model.getEditedPhone().getUid())) {
				phone.setAreaCode(editedPhone.getAreaCode());
				phone.setComment(editedPhone.getComment());
				phone.setCountryCode(editedPhone.getCountryCode());
				phone.setPhoneNumber(editedPhone.getPhoneNumber());
				return;
			}
		}
		model.getEditedEntity().getPhones().add(model.getEditedPhone());
	}

	public void confirmDeletePhoneEvent() {
		model.getEditedEntity().getPhones().remove(model.getEditedPhone());
	}

	// SPECIALIST FEES EVENTS
	public void addSpecialistFeeEvent() {
		model.setEditedSpecialistFee(new SpecialistFee());
	}

	public void confirmSaveSpecialistFeeEvent() {
		SpecialistFee editedSpecialistFee = model.getEditedSpecialistFee();

		for (SpecialistFee specialistFee : model.getEditedEntity().getFees()) {
			if (specialistFee.getUid().equals(model.getEditedSpecialistFee().getUid())) {
				specialistFee.setObservation(editedSpecialistFee.getObservation());
				specialistFee.setAttentionType(editedSpecialistFee.getAttentionType());
				specialistFee.setAmount(editedSpecialistFee.getAmount());
				specialistFee.setAttentionTypeMode(editedSpecialistFee.getAttentionTypeMode());
				return;
			}
		}
		model.getEditedEntity().getFees().add(model.getEditedSpecialistFee());
	}

	public void confirmDeleteSpecialistFeeEvent() {
		model.getEditedEntity().getFees().remove(model.getEditedSpecialistFee());
	}

	public String onFlowProcess(FlowEvent event) {
		return event.getNewStep();
	}
}
